package modularity.passiveDrift;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class FileManager {

	protected String experimentOutputDirectory;
	protected String runOutputDirectory;

	public FileManager(String experimentOutputDirectory) {
		this.experimentOutputDirectory = experimentOutputDirectory;
		createDirectory(this.experimentOutputDirectory);
	}

	public void setRun(int currentRun){
		runOutputDirectory = experimentOutputDirectory + "run" + String.valueOf(currentRun) + "/";

		createDirectory(runOutputDirectory);
	}

	public String getOutputDirectory(int generation) {
		String directory = runOutputDirectory + "generation_" + generation + "/";
		createDirectory(directory);
		return directory;
	}

	private void createDirectory(String dir) {
		File directory = new File(dir);
		if(!directory.exists()){
			directory.mkdirs();
		}
	}

	public void saveArguments(Properties arguments) {
		try {
			arguments.store(new FileOutputStream(experimentOutputDirectory + "arguments.txt"), null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
